<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            clearable
            @clear="getOrderList"
            @keyup.enter.native="getOrderList"
            v-model.trim="queryInfo.query"
          >
            <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表区域 -->
      <el-table :data="orderList" border stripe style="width: 100%">
        <el-table-column type="index" label="序号">
          <template v-slot="scope">
            <span>{{(queryInfo.pagenum - 1) * queryInfo.pagesize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格(元)"></el-table-column>
        <el-table-column label="是否付款">
          <template v-slot="scope">
            <el-button size="mini" v-if="scope.row.pay_status == 0" type="danger">未付款</el-button>
            <el-button size="mini" v-else-if="scope.row.pay_status == 1" type="success">已付款</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <!-- <el-table-column prop="order_fapiao_content" label="发票内容"></el-table-column> -->
        <el-table-column label="下单时间">
          <template
            v-slot="scope"
          >{{new Date(scope.row.create_time) | dateFormat('yyyy-mm-dd HH:MM:ss')}}</template>
        </el-table-column>
        <el-table-column label="操作" width="140px">
          <template v-slot="scope">
            <el-tooltip
              class="item"
              effect="dark"
              :enterable="false"
              content="修改订单地址"
              placement="top"
            >
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="showAdress(scope.row.order_id)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              :enterable="false"
              content="查看物流信息"
              placement="top"
            >
              <el-button type="danger" size="mini" icon="el-icon-location" @click="showProgress()"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页显示 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="adressDialogVisible"
      width="50%"
      @close="adressDialogClosed"
    >
      <el-form ref="addressFormRef" :model="addressForm" :rules="addressFormRules">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="cityData"
            v-model="addressForm.address1"
            @change="handleChange"
            :props="props"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="adressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 查看物流对话框 -->
    <el-dialog title="查看物流" :visible.sync="progressDialogVisible" width="50%">
      <el-timeline :reverse="true">
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="progressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="progressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import cityData from './citydata.js'

export default {
  data() {
    return {
      total: 0,
      // 获取查询参数
      queryInfo: {
        query: '',
        order_fapiao_content: '',
        pagenum: 1,
        pagesize: 10
      },
      orderList: [],
      addressForm: {
        address1: [],
        address2: ''
      },
      addressFormRules: {
        address1: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' },
          { min: 6, max: 40, message: '请填写6至40个字符之间', trigger: 'blur' }
        ]
      },
      adressDialogVisible: false,
      progressDialogVisible: false,
      cityData,
      props: {
        expandTrigger: 'hover'
      },
      progressInfo: []
    }
  },
  methods: {
    async getOrderList() {
      if (this.queryInfo.query) {
        this.queryInfo.pagenum = 1
      }
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getOrderList()
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getOrderList()
    },
    showAdress(id) {
      this.adressDialogVisible = true
    },
    async showProgress() {
      // 1106975712662 供测试使用的物流单号
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.progressInfo = res.data
      console.log(res)
      this.progressDialogVisible = true
    },
    handleChange() {},
    adressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    }
  },
  created() {
    this.getOrderList()
  }
}
</script>
<style lang="less" scoped>
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';
</style>
